package cn.mw.cmdb.template.entity.export;

import cn.mw.cmdb.template.entity.convert.MwMonitorTagConvertDto;
import com.alibaba.excel.annotation.ExcelProperty;
import com.beust.jcommander.Strings;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author gengjb
 * @description 模板导出实体
 * @date 2024/9/3 14:47
 */
@Getter
@Setter
@Schema(name = "TemplateExportDto", description = "模板导出实体")
public class TemplateExportDto {

    private String templateid;

    @ExcelProperty(value = {"模板名称"})
    private String name;

    @ExcelProperty(value = {"链接的模板"})
    private String linkTemplate;

    @ExcelProperty(value = {"标记"})
    private String tag;

    private List<TemplateExportDto> parentTemplates;

    private List<MwMonitorTagConvertDto> tags;

    private List<MonitorItemExportDto> items;

    private List<TriggerExportDto> triggers;

    private List<DiscoverRuleExportDto> discoveries;

    private List<HostExportDto> hosts;

    public void setParentTemplates(List<TemplateExportDto> parentTemplates) {
        this.parentTemplates = parentTemplates;
        if(CollectionUtils.isNotEmpty(parentTemplates)){
            this.linkTemplate = parentTemplates.stream().map(s->s.getName()).collect(Collectors.joining(","));
        }
    }

    public void setTags(List<MwMonitorTagConvertDto> tags) {
        this.tags = tags;
        if(CollectionUtils.isNotEmpty(tags)){
            List<String> tagList = new ArrayList<>();
            for (MwMonitorTagConvertDto mwMonitorTagConvertDto : tags) {
                tagList.add(mwMonitorTagConvertDto.getTag()+mwMonitorTagConvertDto.getValue());
            }
            this.tag = Strings.join(",",tagList);
        }
    }
}
